/*********************************************************
 * copyright learnren.com 版权所有 
 * 开发人员：ivan.yu
 * 创建时间：2013/4/28 16:49:27
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.UI;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Admin
{
    public partial class CatalogueList : BasePage
    {
        CatalogueLogic _CatalogueLogic = null;


        #region BasePage接口

        protected override void InitLogic()
        {
            _CatalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            _CatalogueLogic.Dispose();
            _CatalogueLogic = null;
        }

        #endregion BasePage接口

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            InitCatalogueTree();
            InitCatalogueSelector();
        }

        protected void CatalogueTree1_SelectedNodeChanged(object sender, LEARNREN.COM.WebSite.DelegateAndArges.CatalogueTreeSelectedNodeChangedEventArgs e)
        {
            long id = e.SelectedCatalogueId;

            Catalogue catalogue = _CatalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
            this.txtId.Value = catalogue.Id.ToString();
            this.txtTitle.Text = catalogue.Title;
            this.SelectCatalogueDropDownList1.SelectedCatalogueId = catalogue.ParentId;
            this.ddlType.SelectedValue = catalogue.CatalogueType.ToString();
            this.cbIsRedirect.Checked = catalogue.ReDirect;
            this.txtReDirectUrl.Text = catalogue.ReDirectUrl;
            this.txtHtmlPath.Text = catalogue.HtmlPath;
            this.Order.Text = catalogue.Order.ToString();
            this.KeyWords.Text = catalogue.KeyWords;
            this.Summary.Text = catalogue.Summary;

            btnSaveCatalogue.Enabled = true;
            btnDel.Enabled = true;
        }

        protected void btnSaveCatalogue_Click(object sender, EventArgs e)
        {
            try
            {
                Catalogue catalogue = GetCatalogueForSave(EditStateEnum.Edit);

                var children = _CatalogueLogic.GetChildren(catalogue.Id);

                _CatalogueLogic.Save(catalogue);

                AddScript("showDialog('保存修改成功！');");

                BasicCache.AllCatalogueList.RemoveAll(p => p.Id == catalogue.Id);
                BasicCache.AllCatalogueList.Add(catalogue);

                ReloadCatalogues();
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("showDialog('保存栏目失败！');");
            }
            finally
            {
                ClearControls();
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                string strId = this.txtId.Value;
                long id = 0;
                if (long.TryParse(strId, out id))
                {
                    if (_CatalogueLogic.GetChildren(id).Count() > 0)
                    {
                        AddScript("showDialog('还有下级栏目不能删除！');");
                        return;
                    }

                    Catalogue catalogue = _CatalogueLogic.GetById(id);
                    if (catalogue != null)
                    {
                        _CatalogueLogic.Delete(catalogue);
                        BasicCache.AllCatalogueList.RemoveAll(p => p.Id == catalogue.Id);

                        ReloadCatalogues();
                        ClearControls();
                    }
                }
                AddScript("showDialog('删除成功！');");
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("showDialog('删除失败！');");
            }
        }

        protected void udp2_Load(object sender, EventArgs e)
        {
            AddScript("setCommonStyle();");
        }

        protected void btnSaveAsNew_Click(object sender, EventArgs e)
        {
            try
            {
                Catalogue catalogue = GetCatalogueForSave(EditStateEnum.New);
                _CatalogueLogic.Save(catalogue);
                AddScript("showDialog('保存为新栏目成功！');");

                BasicCache.AllCatalogueList.Add(catalogue);
                ReloadCatalogues();
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("showDialog('保存栏目失败！');");
            }
            finally
            {
                ClearControls();
            }
        }

        #endregion 事件

        #region 私有方法

        private void InitCatalogueSelector()
        {
            SelectCatalogueDropDownList1.CatalogueLogicProvider = _CatalogueLogic;
            SelectCatalogueDropDownList1.CanClickNodeType = CatalogueTypeEnum.All;
        }

        private void InitCatalogueTree()
        {
            CatalogueTree1.CanClickNodeType = CatalogueTypeEnum.All;
            CatalogueTree1.CatalogueLogicProvider = _CatalogueLogic;
            CatalogueTree1.IsShowId = true;
        }

        private void ReloadCatalogues()
        {
            CatalogueTree1.Reload();
            SelectCatalogueDropDownList1.Reload();
        }

        private Catalogue GetCatalogueForSave(EditStateEnum state)
        {
            Catalogue result = null;

            if (state == EditStateEnum.Edit)
            {
                int id = 0;
                if (int.TryParse(this.txtId.Value, out id))
                {
                    result = _CatalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                result = new Catalogue();
                result.Id = _CatalogueLogic.GetNewIdentity();
                result.CreatedTime = DateTime.Now;
            }


            result.Title = txtTitle.Text;
            result.KeyWords = KeyWords.Text;
            result.Summary = Summary.Text;
            result.CatalogueType = (CatalogueTypeEnum)Enum.Parse(typeof(CatalogueTypeEnum), ddlType.SelectedValue);
            result.Order = int.Parse(Order.Text.Trim());
            result.ParentId = SelectCatalogueDropDownList1.SelectedCatalogueId;
            result.Path = _CatalogueLogic.GetPath(result);
            result.ReDirect = cbIsRedirect.Checked;
            result.ReDirectUrl = txtReDirectUrl.Text;
            result.HtmlPath = txtHtmlPath.Text;

            return result;
        }

        private void ClearControls()
        {
            ClearControlText(this.txtId, this.txtTitle, this.Order, this.KeyWords, this.Summary, this.txtHtmlPath, this.txtReDirectUrl, this.cbIsRedirect);
        }

        #endregion 私有方法


    }
}